package com.jtzc.aikf.controller;

import com.jtzc.aikf.store.MongoChatMemoryStore;
import dev.langchain4j.data.message.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author wu chuang
 * @description
 */
@Tag(name = "小矩")
@RestController
@RequestMapping
public class HistoryController {


    @Autowired
    private MongoChatMemoryStore mongoChatMemoryStore;

    @GetMapping("getHistory")
    public List<String> getHistory(@RequestParam("memoryId") Long memoryId) {
        List<ChatMessage> messages = mongoChatMemoryStore.getMessages(memoryId);
        List<String> collect = new ArrayList<>();
        for (ChatMessage message : messages) {
            if (message instanceof UserMessage userMessage) {
                List<Content> contents = userMessage.contents();
                List<String> user = contents.stream()
                        .filter(content -> content instanceof TextContent)
                        .map(t -> ((TextContent) t).text().split("\\n")[0]).collect(Collectors.toList());
                collect.addAll(user);
            }
        }

        return collect;
    }
}
